//
// Created by 莫吉米 on 16/8/10.
//
#include <vector>
#include <mysql.h>
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

#include "Card.h"
#include "Customer.h"
#include "Connector.h"
#include "Adaptor.h"


Adaptor<Card>::Adaptor(Connector& conn):Connect(conn)
{

}

void Adaptor<Card>::Insert(Card& card)
{
    stringstream sqlstream;
    sqlstream<<"INSERT INTO Card(card_type,card_activetime,card_inactivetime,card_count) VALUES(";
    sqlstream<<card.Gettype()<<",\""<<card.Getactivetime().getTimeStr()<<"\",\"";
    sqlstream<<card.Getinactivetime().getTimeStr()<<"\","<<card.Getcount()<<");";
    //cout<<sqlstream.str()<<endl;
    Connect.Update(sqlstream.str());
    return;
}

int Adaptor<Card>::GetLastid() {
    stringstream sqlstream;
    sqlstream<<"SELECT LAST_INSERT_ID()"<<endl;
    int row=Connect.Query(sqlstream.str());
    int col=Connect.FetchRow();
    return Connect.Getint(0);
}


Adaptor<Customer>::Adaptor(Connector& conn):Connect(conn)
{

}

void Adaptor<Customer>::Insert(Customer & customer)
{
    stringstream sqlstream;
    sqlstream<<"INSERT INTO Customer(custom_name,custom_job,custom_address,custom_status) VALUES(";
    sqlstream<<'"'<<customer.Getname()<<"\",\""<<customer.Getjob()<<"\",\""<<customer.Getaddress()<<"\","<<customer.Getstatus()<<");"<<endl;
    //cout<<sqlstream.str()<<endl;
    Connect.Query(sqlstream.str());
}

int Adaptor<Customer>::Getid(string name) {
    stringstream sqlstream;
    sqlstream<<"SELECT custom_id FROM Customer WHERE custom_name=";
    sqlstream<<'"'<<name<<"\";"<<endl;
    //cout<<sqlstream.str()<<endl;
    int row=Connect.Query(sqlstream.str());
    int col=Connect.FetchRow();
    return Connect.Getint(0);

}

void Adaptor<Customer>::BindCard(int customid, int cardid)
{
    stringstream sqlstream;
    sqlstream<<"INSERT INTO Bind(bind_custom_id,bind_card_id) VALUES("<<customid<<","<<cardid<<");";
    Connect.Update(sqlstream.str());
}



